package icom.icss.controller;

import com.alibaba.fastjson.JSONObject;
import icom.icss.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "CheckUsernameServlet",urlPatterns = "/checkUsername")
public class CheckUsernameServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");

        UserServiceImpl userService = new UserServiceImpl();
        boolean b = false;
        try {
               b = userService.checkUsername(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        JSONObject object = new JSONObject();
        object.put("checkUsername",b);


        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(object.toJSONString());

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
